import java.util.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.awt.geom.*;
import java.math.*;
import namespace.*;
public class mainProject 
{
	public static void main(String[] args)
	{
		EventQueue.invokeLater(new Runnable()
		{
			public void run()
			{
				mainFrame frame = new mainFrame();
				frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
				frame.setVisible(true);

			}
		});
	}		
}
class mainFrame extends JFrame
{
	public mainFrame()
	{
		setLocation(1050,50);
		setTitle("Project(KN-35)");
		setSize(dh,dw);
		
		JMenuBar bar = new JMenuBar();
		JMenu mainMenu = new JMenu("Main");
		JMenu helpManu = new JMenu("Help");
		JMenuItem exitItem = new JMenuItem("Exit");
		JMenuItem howItem = new JMenuItem("How to use");
		JMenuItem aboutDev = new JMenuItem("About Developers");
		JMenuItem startItem = new JMenuItem("Start");
		mainMenu.add(startItem);
		helpManu.add(howItem);
		mainMenu.add(aboutDev);
		mainMenu.add(exitItem);
		bar.add(mainMenu);
		bar.add(helpManu);
		add(bar,BorderLayout.NORTH);
		howItem.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent ew)
			{
				howUse how = new howUse();
				how.setVisible(true);
			}
		});
		exitItem.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{
				System.exit(0);
			}
		});
		
		aboutDev.addActionListener(new ActionListener() 
		{
			public void actionPerformed(ActionEvent e2)
			{
				abdevDialog = new AboutDevDialog(mainFrame.this);
				abdevDialog.setVisible(true);
			}
		});	
		final JRadioButton ch1 = new JRadioButton("y = cos(t/sin(x))+N^sin(x)");
		final JRadioButton ch2 = new JRadioButton("y = x^(e^cos(t))/(N+1)^x");
		final JRadioButton ch3 = new JRadioButton("y =  (cosh(t*x)/e^V)+A");
		ch1.setEnabled(false);
		ch2.setEnabled(false);
		ch3.setEnabled(false);
		final JTextArea ar1 = new JTextArea();
		final JTextArea ar2 = new JTextArea();
		final JTextArea ar3 = new JTextArea();
		final JTextArea ar4 = new JTextArea();
		ar1.setText("1");
		ar2.setText("1");
		ar3.setText("1");
		ar4.setText("1");
		ar1.setEnabled(false);
		ar2.setEnabled(false);
		startItem.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent ee)
			{
				ch1.setEnabled(true);
				ch2.setEnabled(true);
				ar1.setEnabled(true);
				ar2.setEnabled(true);
				ch3.setEnabled(true);
			}
		});
		final JLabel l1 = new JLabel("Enter t :");
		final JLabel l2 = new JLabel("Enter N :");
		final JLabel l3 = new JLabel("Enter V :");
		final JLabel l4 = new JLabel("Enter A :");
		JButton b1= new JButton("to build ");
		b2 = new JButton("Close all"); 
		b2.setEnabled(false);
		eastPanel = new JPanel();
		eastPanel.setLayout(new GridLayout(15,15));
		eastPanel.add(ch1);
		eastPanel.add(ch2);
		eastPanel.add(ch3);
		
		
		
		add(eastPanel,BorderLayout.WEST);
		southPanel = new JPanel();
		southPanel.add(b1);
		southPanel.add(b2);
		add(southPanel,BorderLayout.SOUTH);
		
		ch1.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{
				
					eastPanel.remove(l4);
					eastPanel.remove(ar4);
					eastPanel.remove(l2);
		    		eastPanel.remove(l3);
		    		eastPanel.remove(ar1);
					eastPanel.remove(ar2);
					eastPanel.remove(ar3);
					ch2.setSelected(false);
					ch3.setSelected(false);
					eastPanel.add(l1);
					eastPanel.add(ar1);
					eastPanel.add(l2);
					eastPanel.add(ar2);
					eastPanel.updateUI();
					
			}
		});
		ch2.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{
				
					eastPanel.remove(l4);
					eastPanel.remove(ar4);
					eastPanel.remove(l1);
			    	eastPanel.remove(l3);
			    	eastPanel.remove(ar1);
					eastPanel.remove(ar2);
					eastPanel.remove(ar3);
			    	ch1.setSelected(false);
					ch3.setSelected(false);
					eastPanel.add(l1);
					eastPanel.add(ar1);
					eastPanel.add(l2);
					eastPanel.add(ar2);
					eastPanel.updateUI();
				
			}
		});
		ch3.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{
					
				    eastPanel.remove(l1);
				    eastPanel.remove(l2);
				    eastPanel.remove(ar1);
				    eastPanel.remove(ar2);
				    eastPanel.remove(ar3);
					ch1.setSelected(false);
					ch2.setSelected(false);
					eastPanel.add(l1);
					eastPanel.add(ar1);
					eastPanel.add(l3);
					eastPanel.add(ar3);
					eastPanel.add(l4);
					eastPanel.add(ar4);
					eastPanel.updateUI();
				
			}
		});
	
		b1.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent e3)
			{
				if(ch1.isSelected())
				{
					try
					{
						String gt = ar1.getText();
						String gN = ar2.getText();
						//String gV = ar3.getText();
						double t = Double.parseDouble(gt);
						//int V = (int)Double.parseDouble(gV);
						double N = Double.parseDouble(gN);
						if((N>=0) && (t<10 ||t>10))
						{
							fr = new drawFunc(t,N);
							fr.setVisible(true);
							b2.setEnabled(true);
							
						}
						else
						{
							erFrame frame1 = new erFrame();
							frame1.setVisible(true);
							
						}
					}
					catch(NumberFormatException d)
					{
						ObDialog dv = new ObDialog(mainFrame.this);
						dv.setVisible(true);
					}
				}
				if(ch2.isSelected())
				{
					try
					{
						String gt = ar1.getText();
						String gN = ar2.getText();
						//String gV = ar3.getText();
						double t = Double.parseDouble(gt);
						//int V = (int)Double.parseDouble(gV);
						double N = Double.parseDouble(gN);
						if((N>=0) && (t<10 ||t>10))
						{
							fr1 = new drawFunc1(t,N);
							fr1.setVisible(true);
							b2.setEnabled(true);
						}
						else
						{
							frame = new erFrame();
							frame.setVisible(true);
						}
					}
					catch(NumberFormatException d)
					{
						ObDialog dv = new ObDialog(mainFrame.this);
						dv.setVisible(true);
					}
				}
				if(ch3.isSelected())
				{
					try
					{
						String gt = ar1.getText();
						//String gN = ar2.getText();
						String gA = ar3.getText();
						double t = Double.parseDouble(gt);
						int A = (int)Double.parseDouble(gA);
						String gV = ar4.getText();
						double V = Double.parseDouble(gV);
						if((V>0 && V<60) && (t<10 ||t>10 ) && (A>=0))
						{
							fr2 = new drawFunc2(t,V,A);
							fr2.setVisible(true);
							b2.setEnabled(true);
						}
						else
						{
							frame1 = new erFrame1();
							frame1.setVisible(true);
						}
						
					}
					catch(NumberFormatException d)
					{
						ObDialog dv = new ObDialog(mainFrame.this);
						dv.setVisible(true);
					}
				}
				
			}
		});	
		b2.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent action)
			{

				try
				{
					fr.setVisible(false);
					fr1.setVisible(false);
					fr2.setVisible(false);
					frame.setVisible(false);
					frame1.setVisible(false);
				}
				catch(NullPointerException eee)
				{}
				
			}
		});
	
	}
	public class KeyAction extends AbstractAction
	{
		public KeyAction(String name,JTextArea aa)
		{
			aa2 = aa;
			putValue(Action.NAME,name);
		}
		public void actionPerformed(ActionEvent e)
		{
			aa2.setCursor(null);
		}
		private JTextArea aa2;
	}
	private JButton b2;
	private erFrame frame;
	private erFrame1 frame1;
	private int n1 = 0;
	private drawFunc fr;
	private drawFunc1 fr1;
	private drawFunc2 fr2;
	private JPanel southPanel;
	private JPanel grPanel;
	private AboutDevDialog abdevDialog;
	private JPanel eastPanel;
	private JPanel buttonPanel;
	private static final int dh = 300;
	private static final int dw = 400;
}



class AboutDevDialog extends JDialog
{
	public AboutDevDialog(JFrame owner)
	{
		super(owner,"About developer",true);
		JLabel label1 = new JLabel("KN - 35 SAPR Lviv 2011");
		JLabel label2 = new JLabel(" :)");
		add(label2,BorderLayout.NORTH);
		add(label1,BorderLayout.SOUTH);
		JButton yes = new JButton("OK");
		JPanel panel = new JPanel();
		panel.add(yes);
		add(panel);
		yes.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{
				setVisible(false);
			}
		});
		setSize(350,150);
		setLocation(950,460);
	}	
}
class ObDialog extends JDialog
{
	public ObDialog(JFrame owner)
	{
		super(owner,"Warning",true);
		JLabel label1 = new JLabel("Please, enter all right data !");
		add(label1,BorderLayout.NORTH);
		JButton yes = new JButton("OK");
		JPanel panel = new JPanel();
		panel.add(yes);
		add(panel);
		
		yes.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent e11)
			{
				setVisible(false);
			}
		});
		setLocation(950,460);
		setSize(350, 150);
	}	
}
class erFrame extends JFrame
{
	public erFrame()
	{
		setTitle("Warning");
		setSize(400,250);
		setLocation(950, 460);
		panel = new JPanel();
		panel.setLayout(new GridLayout(10,10));
		panel.add(new JLabel("Parameter t is real type :   -10<t<10"));
		panel.add(new JLabel(" "));
		panel.add(new JLabel("Parameter N is real type:     N>=0" ));
		panel.add(new JLabel(" "));
		panel.add(new JLabel("Please, enter right date"));
		add(panel);	
		b = new JButton("Close");
		b.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent er)
			{
				setVisible(false);
			}
		});
		add(b,BorderLayout.SOUTH);
	}
	private JPanel panel;
	private JButton b;
}
class erFrame1 extends JFrame
{
	public erFrame1()
	{
		setTitle("Warning");
		setSize(400,250);
		setLocation(950, 460);
		panel = new JPanel();
		panel.setLayout(new GridLayout(10,10));
		panel.add(new JLabel("Parameter t is real type :   -10<t<10"));
		panel.add(new JLabel(" "));
		panel.add(new JLabel("Parameter V is real type:      0<v<60" ));
		panel.add(new JLabel(" "));
		panel.add(new JLabel("Parameter A is natural type:      A>=0" ));
		panel.add(new JLabel(" "));
		panel.add(new JLabel("Please, enter right date"));
		add(panel);	
		b = new JButton("Close");
		b.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent er)
			{
				setVisible(false);
			}
		});
		add(b,BorderLayout.SOUTH);
	}
	private JPanel panel;
	private JButton b;
}
class howUse extends JFrame
{
	public howUse()
	{
		setLocation(950, 460);
		setSize(400,250);
		setTitle("How to use ");
		panel = new JPanel();
		panel.setLayout(new GridLayout(10,10));
		panel.add(new JLabel(" "));
		panel.add(new JLabel("This program is for drawing graphs. "));
		panel.add(new JLabel("For start you have to select the menu: main "));
		panel.add(new JLabel("then select : start "));
		panel.add(new JLabel("In the fields define the relevant parameters of functions "));
		panel.add(new JLabel(" "));
		add(panel);
		b2 = new JButton("Ok");
		b2.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent er)
			{
				setVisible(false);
			}
		});
		add(b2,BorderLayout.SOUTH);
	}
	private JPanel panel;
	private JButton b2;
}